package com.contactdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.contactdemo.bean.Contact;
import com.contactdemo.widget.SideBar;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {


    private RecyclerView rv_content;


    //原始数据
    private ArrayList<Contact> datas = new ArrayList<>();
    private ContactAdapter mAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
        initSidBar();

    }



    public void initSidBar() {
        rv_content = (RecyclerView) findViewById(R.id.rv_content);
        rv_content.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        //分割线
        mAdapter = new ContactAdapter(R.layout.item_contact_layout, new ArrayList<Contact>());
        rv_content.setAdapter(mAdapter);
        rv_content.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this,position+"",Toast.LENGTH_LONG).show();
            }
        });
        SideBar mSideBar = (SideBar) findViewById(R.id.school_friend_sidrbar);
        TextView mDialog = (TextView) findViewById(R.id.school_friend_dialog);
        EditText mSearchInput = (EditText) findViewById(R.id.school_friend_member_search_input);
        mSideBar.setTextView(mDialog);
        //滑动侧边栏监听
        mSideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {

                int position = 0;
                // 该字母首次出现的位置
                if (mAdapter != null) {
                    position = mAdapter.getPositionForSection(s.charAt(0));
                }
                if (position != -1) {
                      rv_content.scrollToPosition(position);
                } else if (s.contains("#")) {
                    rv_content.scrollToPosition(0);

                }
            }
        });
        mSearchInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                ArrayList<Contact> temp = new ArrayList<>(datas);
                for (Contact data : datas) {
                    if (data.getName().contains(s) || data.getPinyin().contains(s))
                    {
                    } else {
                        temp.remove(data);
                    }
                }
                if (mAdapter != null) {
                    mAdapter.setNewData(temp);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        parser();
    }



    private void parser()
    {

        try {
             String[] strs = new String[]{"a","b","c","d","e","f","g","h","i","j"};
            for (int i = 0; i <20 ; i++) {
                Contact data = new Contact();
                String s =strs[((int)(Math.random()*10))];
                data.setName(s+"name");
                data.setUrl("portrait");
                data.setId(i);
                data.setPinyin(HanziToPinyin.getPinYin(data.getName()));
                datas.add(data);
            }

            Contact data = new Contact();
            data.setName(0+"name");
            data.setUrl("portrait");
            data.setId(5);
            data.setPinyin(HanziToPinyin.getPinYin(data.getName()));
            datas.add(data);

            //排序
            Collections.sort(datas);
            mAdapter.setNewData(datas);
        } catch (Exception e) {
            Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
        }
    }


}
